import { requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { useCallback, useMemo } from 'react';
import { ManageAPI } from '../common/type';
import { DetailManageSearchColumns } from '../mconfig';
import { getBaseUrl } from '@/services/urlconst';
let baseUrl = getBaseUrl();

const DetailManageServices = (curRes: Record<string, any>, detailInfo) => {
  /**
   * 获取数据字典
   * */
  const loadMdicIds: string[] = [
    // `${getTenantID()}${ILoadMapTypeAll.PURCHASE_MATERIAL}`,
  ];

  /**
   * 获取数据字典loadMdicMap
   * */
  // const { loadMapInfo: loadMdicMapList } = getLoadMapList(loadMdicIds);

  /**
   * 账期
   * */
  const { depreciationDate } = detailInfo;

  /**
   * 管理页获取数据方法
   * */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (reqParams: ManageAPI.IAssetTableRequestType) => {
      let reqUrl = `${baseUrl}/assetDepreciationCalculation/detail`;

      let reqBody = {
        current: 1,
        pageSize: 10,
        // dicTypeIds: loadMdicIds?.toString(),
        depreciationDate,
        ...reqParams,
      };

      return requestFunComponFiex('POST', reqUrl, reqBody);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /**
   * 重新请求的方法
   * */
  const reloadRequest = useCallback((reqParams: ManageAPI.IAssetTableRequestType | any) => {
    let reqBody = {
      ...reqParams,
    };

    if (reqBody?.entryDate) {
      const [entryDateStart, entryDateEnd] = reqBody.entryDate;
      reqBody['entryDateStart'] = entryDateStart.format('YYYY-MM-DD 00:00:00');
      reqBody['entryDateEnd'] = entryDateEnd.format('YYYY-MM-DD 23:59:59');
      delete reqBody['entryDate'];
    }

    run({ ...reqBody });
  }, []);

  /**
   * 页面数据及分页信息
   * */
  const tableValues = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /**
   * 搜索功能配置columns
   * */
  const searchList = useMemo(() => {
    return {
      // loadMdicMapList,
      searchList: DetailManageSearchColumns(),
    };
  }, []); //loadMdicMapList

  return {
    loading,
    reloadRequest,
    reloadManageData: refresh,
    ...tableValues, //页面列表值
    ...searchList,
  };
};

export { DetailManageServices };
